import { BaseWindow } from "../../../../component/window/BaseWindow";
import { ILayer } from "../../../../mvc/interfaces/ILayer";
import { PageTool } from "../../../../common/global/PageTool";
import { GameConst } from "../../../../component/gconst/GameConst";
import { Language } from "../../../../../extend/language/Language";
import { SPetInfo } from "../../../../../Message/Game/SPetInfo";
import { ToolTipsManager } from "../../../../../com/mui/manager/ToolTipsManager";
import { ImageTitleToolTip } from "../../common/tooltip/ImageTitleToolTip";
import { ObjCreate } from "../../common/ObjCreate";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { TilePanel } from "../../equipment/TilePanel";
import { PetChangeModelCellRenderer } from "./PetChangeModelCellRenderer";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { ListEvent } from "../../../../../fl/events/ListEvent";
import { PageSelecter } from "../../common/pageSelect/PageSelecter";
import { ShopConfig } from "../../../resource/ShopConfig";
import { ECategory } from "../../../../../Message/Public/ECategory";
import { EProp } from "../../../../../Message/Public/EProp";
import { EPrictUnit } from "../../../../../Message/Public/EPrictUnit";
import { RefreshMaterialItem } from "../../equipment/popupPanels/RefreshMaterialItem";
import { LoaderHelp } from "../../../../common/display/LoaderHelp";
import { ResFileConst } from "../../../resource/ResFileConst";
import { ImagesConst } from "../../../resource/ImagesConst";
import { PetChangeModelData } from "./PetChangeModelData";
import { MsgManager } from "../../../manager/MsgManager";
import { PulseSharedObject } from "../../player/PulseSharedObject";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { CheckBoxWin } from "../../common/alertwins/CheckBoxWin";
import { ImageTitleToolTipData } from "../../common/tooltip/itemData/ImageTitleToolTipData";
import { Cache } from "../../../cache/Cache";
import { EquipmentUtil } from "../../common/util/EquipmentUtil";
import { PetUtil } from "../info/PetUtil";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { GCheckBox } from "../../../../../com/mui/controls/GCheckBox";
type int = number;
//class PetChangeModelWindow
    
    export  class PetChangeModelWindow extends BaseWindow
    {
        constructor(arg1: ILayer=null)
        {
            super(arg1);
            this._pageTool = new PageTool([], 6);
            this._cost = GameConst.PetChangeModelCost;
            this.setSize(645, 540);
            this.titleHeight = 28;
            this.title = Language.getString(54807);
            return;
        }

        public updateByPetinfo(arg1: SPetInfo): void
        {
            this._petInfo = arg1;
            return;
        }

        public updateList(arg1: Array<any>): void
        {
            this._pageTool.array = arg1;
            this._pageBar.maxPage = this._pageTool.pagesCount;
            this.onPageChangeHandler(null);
            ToolTipsManager.unregister(this._spBtn);
            ToolTipsManager.register(this._spBtn, ImageTitleToolTip, this.tooltip);
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc1=null;
            super.createChildren();
            ObjCreate.createBg(17, 39, 610, 442, this);
            loc1 = new GTextFormat(FontUtil.lishuName, 18, 16776960);
            var loc2=ObjCreate.createGTextField("", 0, 46, 645, 20, this, loc1);
            loc2.autoSize = flash.text.TextFieldAutoSize.CENTER;
            loc2.text = Language.getString(54808);
            this._petList = new TilePanel();
            this._petList.setSize(620, 384);
            this._petList.columnWidth = 200;
            this._petList.rowHeight = 192;
            this._petList.setStyle("cellRenderer", PetChangeModelCellRenderer);
            this._petList.move(20, 70);
            this._petList.dataProvider = new DataProvider([0, 0, 0, 0, 0, 0]);
            this._petList.addEventListener(ListEvent.ITEM_CLICK, this.onSelectItemHandler);
            this.addChild(this._petList);
            this._pageBar = new PageSelecter();
            this._pageBar.x = (645 - this._pageBar.width) / 2;
            this._pageBar.y = 455;
            this._pageBar.addEventListener(flash.events.Event.CHANGE, this.onPageChangeHandler);
            this.addChild(this._pageBar);
            var loc3=ObjCreate.createGTextField(Language.getString(54809), 30, 495, 100, 28, this, loc1);
            var loc4=ShopConfig.instance.getGoldItemByType(ECategory._ECategoryProp, EProp._EPropPetCollectAttrActivate, -1, EPrictUnit._EPriceUnitGold, 1702);
            this._material = new RefreshMaterialItem();
            this._material.updateData(loc4);
            this._material.needAmount = 0;
            ObjCreate.setObjAttri(this._material, 154, 490, -1, -1, this);
            this._useBindCB = ObjCreate.createCheckBox(Language.getString(70186), 300, 506, 121, 20, this);
            this._autoBuyCB = ObjCreate.createCheckBox(Language.getString(70187), 300, 484, 121, 20, this);
            this._changeBtn = ObjCreate.createGButton(Language.getString(54910), 540, 496, 70, 24, this, "ButtonRed");
            this._changeBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onChangeBtnClickHandler);
            this._activateBtn = ObjCreate.createGButton(Language.getString(54933), 450, 496, 70, 24, this, "ButtonRed");
            this._activateBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onActivateBtnClickHandler);
            this._spBtn = new Object /* flash.display.Sprite */();
            ObjCreate.setObjAttri(this._spBtn, 590, 40, -1, -1, this);
            LoaderHelp.addResCallBack(ResFileConst.PetHua, this.onLoadedHandler);
            return;
        }

        /* internal  */onLoadedHandler(): void
        {
            ObjCreate.createGBitmap(ImagesConst.PetChangeModelTotalAttr, 0, 0, this._spBtn);
            return;
        }

        /* internal  */onPageChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            this._petList.selectedIndex = -1;
            var loc1=this._pageBar.currentPage;
            var loc2=this._pageTool.getByPageNo(loc1);
            var loc3=[];
            var loc4=0;
            while (loc4 < 6 - loc2.length) 
            {
                loc3.push(new PetChangeModelData());
                ++loc4;
            }
            loc3 = loc2.concat(loc3);
            this._petList.dataProvider = new DataProvider(loc3);
            return;
        }

        /* internal  */onChangeBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var e: Object /* flash.events.MouseEvent */;
            var selectedItem: Object;
            var toCode: int;
            var isNeedAlert: Boolean;
            var onBtnClickHandler: Function;

            var loc1;
            toCode = 0;
            isNeedAlert = false;
            e = arg1;
            selectedItem = this._petList.selectedItem;
            if (selectedItem) 
            {
                toCode = this._petList.selectedItem["code"] as int;
            }
            if (toCode == 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(54911));
                return;
            }
            if (this._petInfo) 
            {
                isNeedAlert = !PulseSharedObject.isTodayNotTips("PetChangeModel");
                if (isNeedAlert) 
                {
                    onBtnClickHandler = (arg1: int, arg2: Boolean): void
                    {
                        if (arg1 == Alert.OK) 
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.PetChangeModel, {"petUid": _petInfo.uid, "petCode": toCode}));
                            Alert.resetToDefault();
                        }
                        if (arg2) 
                        {
                            PulseSharedObject.save("PetChangeModel", true);
                        }
                        return;
                    }
                    Alert.alertWinRenderer = CheckBoxWin;
                    Alert.show(Language.getStringByParam(54912, this._cost), null, Alert.OK | Alert.CANCEL, null, onBtnClickHandler);
                }
                else 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetChangeModel, {"petUid": this._petInfo.uid, "petCode": toCode}));
                }
            }
            return;
        }

        /* internal  */onActivateBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc2=0;
            var loc1=this._petList.selectedItem;
            if (loc1) 
            {
                loc2 = this._petList.selectedItem["code"] as int;
            }
            if (loc2 == 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(54934));
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetActivateModel, {"code": loc2, "auto": this._autoBuyCB.selected, "bind": this._useBindCB.selected}));
            return;
        }

        /* internal  */onSelectItemHandler(arg1: ListEvent=null): void
        {
            var loc1=null;
            if (arg1) 
            {
                loc1 = arg1.item as PetChangeModelData;
            }
            else 
            {
                loc1 = this._petList.selectedItem as PetChangeModelData;
            }
            if (loc1 && loc1.code > 0) 
            {
                this._activateBtn.focusEnabled = true;
                this._material.needAmount = loc1.materialNum;
            }
            else 
            {
                this._activateBtn.focusEnabled = false;
                this._material.needAmount = 0;
            }
            return;
        }

        /* internal  */onPackChangeHandler(arg1: Object): void
        {
            this.onSelectItemHandler();
            return;
        }

        /* internal  */get tooltip(): ImageTitleToolTipData
        {
            var loc1=null;
            var loc4=null;
            var loc7=null;
            var loc8=0;
            var loc2=Cache.instance.role.entityInfo.career;
            var loc3=[];
            var loc5="<font color=\'#00ff00\'>";
            var loc6=new ImageTitleToolTipData();
            var loc9=0;
            var loc10=this._pageTool.array;
            for(loc7 of loc10) 
            {
                if (loc7.activate != 1) 
                {
                    continue;
                }
                loc3.push(loc7.addAttr);
            }
            loc4 = EquipmentUtil.getSelfCareerAttribute(loc2, PetUtil.getAttr(loc3));
            loc8 = 0;
            while (loc8 < loc4.length) 
            {
                loc1 = loc4[loc8];
                loc5 = loc5 + "     " + loc1.name + "     +" + loc1.value + "\n";
                ++loc8;
            }
            loc5 = loc5 + "</font>";
            loc6.title = ImagesConst.PetChangeModelAttrTotalTip;
            loc6.tip = loc5;
            return loc6;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show(arg1, arg2);
            this._petList.selectedIndex = -1;
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetChangeModelGetList));
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.onPackChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onPackChangeHandler);
            return;
        }

        public /* override */ hide(): void
        {
            super.hide();
            NetDispatcher.removeCmdListener(ServerCommand.BackpackDataChange, this.onPackChangeHandler);
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.onPackChangeHandler);
            return;
        }

        /* internal */ /* var */_petList: TilePanel;

        /* internal */ /* var */_pageBar: PageSelecter;

        /* internal */ /* var */_changeBtn: GButton;

        /* internal */ /* var */_activateBtn: GButton;

        /* internal */ /* var */_pageTool: PageTool;

        /* internal */ /* var */_petInfo: SPetInfo;

        /* internal */ /* var */_cost: int;

        /* internal */ /* var */_material: RefreshMaterialItem;

        /* internal */ /* var */_useBindCB: GCheckBox;

        /* internal */ /* var */_autoBuyCB: GCheckBox;

        /* internal */ /* var */_spBtn: Object /* flash.display.Sprite */;
    }
